﻿using System;
using System.IO;
using System.Web;
using Soveryn.Commerce.Shared;
using System.Linq;
using Soveryn.Commerce.BaseClasses;

namespace Soveryn.Commerce.Pages
{
    public partial class ListFiles : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

            Response.ContentType = "application/json";
            Response.Cache.SetAllowResponseInBrowserHistory(false);
            Response.Cache.SetCacheability(HttpCacheability.NoCache);
            Response.Cache.SetNoStore();

            if (String.IsNullOrEmpty(Request.QueryString["EmployeeToken"]) || String.IsNullOrEmpty(Request.QueryString["Path"]))
                return;

            if (PageBaseClass.EmployeeExternalSessionValid(Request.QueryString["EmployeeToken"]))
            {

                String path = Request.QueryString["Path"];
                if (Directory.Exists(Server.MapPath(path)))
                {

                    DirectoryInfo di = new DirectoryInfo(Server.MapPath(path));
                    FileSystemInfo[] fileList = di.GetFileSystemInfos();
                    var orderedFiles = fileList.OrderByDescending(f => f.CreationTime);

                    String[] files = new String[fileList.Length];
                    var index = 0;
                    foreach (FileSystemInfo f in orderedFiles)
                    {
                        files[index] = f.Name;
                        index++;
                    }

                    Response.Write(StringUtils.EncodeTo64(String.Join(",", files)));

                }

            }

        }

    }
}